package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2020/8/29
 * @description
 **/
public class OrderOperateView extends LinearLayout {

    public static final int OPERATE_TOP_LEFT = 0;
    public static final int OPERATE_TOP_RIGHT = 1;
    public static final int OPERATE_BOTTOM_LEFT = 2;
    public static final int OPERATE_BOTTOM_RIGHT = 3;
    public static final int OPERATE_CENTER = 4;

    private int topLeftBg;
    private int topRightBg;
    private int bottomLeftBg;
    private int bottomRightBg;

    private int topLeftImage;
    private int topRightImage;
    private int bottomLeftImage;
    private int bottomRightImage;

    private int centerBg;
    private int centerWidth;
    private int centerHeight;

//    private ImageView centerOperateImageView;
    private TextView centerOperateTextView;
    private TextView oneOperateTextView;
    private TextView twoOperateTextView;
    private TextView threeOperateTextView;
    private TextView fourOperateTextView;

    private ImageView topLeftImageView;
    private ImageView topRightImageView;
    private ImageView bottomLeftImageView;
    private ImageView bottomRightImageView;

    private LinearLayout topLeftLinearLayout;
    private LinearLayout topRightLinearLayout;
    private LinearLayout bottomLeftLinearLayout;
    private LinearLayout bottomRightLinearLayout;

    private List<TextView> operateTextViewList = new ArrayList<>();
    private List<ImageView> operateImageList = new ArrayList<>();
    private List<OrderOperateInfo> operateInfoList = new ArrayList<>();
    private OnOrderOperateClickListener onOrderOperateClickListener;
    private OrderOperateInfo centerOperateInfo;

    public OrderOperateView(Context context) {
        this(context, null);
    }

    public OrderOperateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OrderOperateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OrderOperateView);
        boolean isShowCenterOperate;
        try {
            isShowCenterOperate = typedArray.getBoolean(R.styleable.OrderOperateView_isShowCenterOperate, false);
            topLeftBg = typedArray.getResourceId(R.styleable.OrderOperateView_OperateTopLeftBackground, R.drawable.bg_order_operate);
            topRightBg = typedArray.getResourceId(R.styleable.OrderOperateView_OperateTopRightBackground, R.drawable.bg_order_operate);
            bottomLeftBg = typedArray.getResourceId(R.styleable.OrderOperateView_OperateBottomLeftBackground, R.drawable.bg_order_operate);
            bottomRightBg = typedArray.getResourceId(R.styleable.OrderOperateView_OperateBottomRightBackground, R.drawable.bg_order_operate);

            topLeftImage = typedArray.getResourceId(R.styleable.OrderOperateView_OperateTopLeftImage, 0);
            topRightImage = typedArray.getResourceId(R.styleable.OrderOperateView_OperateTopRightImage, 0);
            bottomLeftImage = typedArray.getResourceId(R.styleable.OrderOperateView_OperateBottomLeftImage, 0);
            bottomRightImage = typedArray.getResourceId(R.styleable.OrderOperateView_OperateBottomRightImage, 0);

            centerBg = typedArray.getResourceId(R.styleable.OrderOperateView_OperateCenterBackground, R.drawable.bg_order_center_operate);
            centerWidth = typedArray.getDimensionPixelSize(R.styleable.OrderOperateView_OperateCenterWidth, -2);
            centerHeight = typedArray.getDimensionPixelSize(R.styleable.OrderOperateView_OperateCenterHeight, -2);
        } finally {
            typedArray.recycle();
        }

        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_order_operate, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        oneOperateTextView = view.findViewById(R.id.layout_order_operate_oneTextView);
        twoOperateTextView = view.findViewById(R.id.layout_order_operate_twoTextView);
        threeOperateTextView = view.findViewById(R.id.layout_order_operate_threeTextView);
        fourOperateTextView = view.findViewById(R.id.layout_order_operate_fourTextView);
//        centerOperateImageView = view.findViewById(R.id.layout_order_operate_centerImageView);
        centerOperateTextView = view.findViewById(R.id.layout_order_operate_centerTextView);


        topLeftImageView = view.findViewById(R.id.layout_order_operate_topLeftImageView);
        topRightImageView = view.findViewById(R.id.layout_order_operate_topRightImageView);
        bottomLeftImageView = view.findViewById(R.id.layout_order_operate_bottomLeftImageView);
        bottomRightImageView = view.findViewById(R.id.layout_order_operate_bottomRightImageView);

        topLeftLinearLayout = view.findViewById(R.id.layout_order_operate_topLeftLinearLayout);
        topRightLinearLayout = view.findViewById(R.id.layout_order_operate_topRightLinearLayout);
        bottomLeftLinearLayout = view.findViewById(R.id.layout_order_operate_bottomLeftLinearLayout);
        bottomRightLinearLayout = view.findViewById(R.id.layout_order_operate_bottomRightLinearLayout);

        operateTextViewList.add(oneOperateTextView);
        operateTextViewList.add(twoOperateTextView);
        operateTextViewList.add(threeOperateTextView);
        operateTextViewList.add(fourOperateTextView);


        operateImageList.add(topLeftImageView);
        operateImageList.add(topRightImageView);
        operateImageList.add(bottomLeftImageView);
        operateImageList.add(bottomRightImageView);

        setTopLeftBg(topLeftBg);
        setTopRightBg(topRightBg);
        setBottomLeftBg(bottomLeftBg);
        setBottomRightBg(bottomRightBg);

        setTopLeftImage(topLeftImage);
        setTopRightImage(topRightImage);
        setBottomLeftImage(bottomLeftImage);
        setBottomRightImage(bottomRightImage);

        setShowCenterOperate(isShowCenterOperate);
        setCenterBg(centerBg);
//        setCenterSize(centerWidth, centerHeight);
        setCenterTextViewSize(centerWidth, centerHeight);
        initListener(context);
    }

//    private void setCenterSize(int centerWidth, int centerHeight) {
//        ViewGroup.LayoutParams layoutParams = centerOperateImageView.getLayoutParams();
//        if (centerWidth == -1 && centerHeight == -1) {
//            layoutParams.width = LayoutParams.MATCH_PARENT;
//            layoutParams.height = LayoutParams.MATCH_PARENT;
//        } else if (centerWidth == -2 && centerHeight == -2) {
//            layoutParams.width = LayoutParams.WRAP_CONTENT;
//            layoutParams.height = LayoutParams.WRAP_CONTENT;
//        } else if (centerWidth == -1 && centerHeight == -2) {
//            layoutParams.width = LayoutParams.MATCH_PARENT;
//            layoutParams.height = LayoutParams.WRAP_CONTENT;
//        } else if (centerWidth == -2 && centerHeight == -1) {
//            layoutParams.width = LayoutParams.WRAP_CONTENT;
//            layoutParams.height = LayoutParams.MATCH_PARENT;
//        } else {
//            layoutParams.width = centerWidth;
//            layoutParams.height = centerHeight;
//        }
//        centerOperateImageView.setLayoutParams(layoutParams);
//    }

    private void setCenterTextViewSize(int centerWidth, int centerHeight) {
        ViewGroup.LayoutParams layoutParams = centerOperateTextView.getLayoutParams();
        if (centerWidth == -1 && centerHeight == -1) {
            layoutParams.width = LayoutParams.MATCH_PARENT;
            layoutParams.height = LayoutParams.MATCH_PARENT;
        } else if (centerWidth == -2 && centerHeight == -2) {
            layoutParams.width = LayoutParams.WRAP_CONTENT;
            layoutParams.height = LayoutParams.WRAP_CONTENT;
        } else if (centerWidth == -1 && centerHeight == -2) {
            layoutParams.width = LayoutParams.MATCH_PARENT;
            layoutParams.height = LayoutParams.WRAP_CONTENT;
        } else if (centerWidth == -2 && centerHeight == -1) {
            layoutParams.width = LayoutParams.WRAP_CONTENT;
            layoutParams.height = LayoutParams.MATCH_PARENT;
        } else {
            layoutParams.width = centerWidth;
            layoutParams.height = centerHeight;
        }
        centerOperateTextView.setLayoutParams(layoutParams);
    }

    public void setTopLeftBg(int bg) {
        if (bg != 0) {
            topLeftLinearLayout.setBackgroundResource(bg);
        }
    }

    public void setTopRightBg(int bg) {
        if (bg != 0) {
            topRightLinearLayout.setBackgroundResource(bg);
        }
    }

    public void setBottomLeftBg(int bg) {
        if (bg != 0) {
            bottomLeftLinearLayout.setBackgroundResource(bg);
        }
    }

    public void setBottomRightBg(int bg) {
        if (bg != 0) {
            bottomRightLinearLayout.setBackgroundResource(bg);
        }
    }

    public void setTopLeftImage(int imageId) {
        if (imageId != 0) {
            topLeftImageView.setBackgroundResource(imageId);
        }
    }

    public void setTopRightImage(int imageId) {
        if (imageId != 0) {
            topRightImageView.setBackgroundResource(imageId);
        }
    }

    public void setBottomLeftImage(int imageId) {
        if (imageId != 0) {
            bottomLeftImageView.setBackgroundResource(imageId);
        }
    }

    public void setBottomRightImage(int imageId) {
        if (imageId != 0) {
            bottomRightImageView.setBackgroundResource(imageId);
        }
    }


    public void setCenterBg(int bg) {
        if (bg != 0) {
//            centerOperateImageView.setBackgroundResource(bg);
            centerOperateTextView.setBackgroundResource(bg);
        } else {
            centerOperateTextView.setBackgroundResource(0);
//            centerOperateImageView.setBackgroundResource(0);
        }
    }

    public void setCenterText(String text) {
        if (centerOperateTextView != null) {
            centerOperateTextView.setText(text);
        }
    }


    private void initListener(Context context) {

        topLeftLinearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                operateClick(context, oneOperateTextView, OPERATE_TOP_LEFT);
            }
        });
        topRightLinearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                operateClick(context, twoOperateTextView, OPERATE_TOP_RIGHT);

            }
        });
        bottomLeftLinearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                operateClick(context, threeOperateTextView, OPERATE_BOTTOM_LEFT);
            }
        });
        bottomRightLinearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                operateClick(context, fourOperateTextView, OPERATE_BOTTOM_RIGHT);
            }
        });
//        centerOperateImageView.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onOrderOperateClickListener != null) {
//                    onOrderOperateClickListener.onOperateClick(context, null, OPERATE_CENTER, centerOperateInfo);
//                }
//            }
//        });
        centerOperateTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onOrderOperateClickListener != null) {
                    onOrderOperateClickListener.onOperateClick(context, null, OPERATE_CENTER, centerOperateInfo);
                }
            }
        });
    }

    private void operateClick(Context context, TextView textView, int position) {
        int size = operateInfoList.size();

        if (position >= size) return;
        if (onOrderOperateClickListener != null) {
            onOrderOperateClickListener.onOperateClick(context, textView, position, operateInfoList.get(position));
        }
    }

    private void setShowCenterOperate(boolean isShowCenterOperate) {
        if (isShowCenterOperate) {
//            centerOperateImageView.setVisibility(View.GONE);
            centerOperateTextView.setVisibility(View.VISIBLE);
        } else {
//            centerOperateImageView.setVisibility(View.GONE);
            centerOperateTextView.setVisibility(View.GONE);
        }
    }

    public void setOperateInfoList(List<OrderOperateInfo> operateInfoList) {
        this.operateInfoList = operateInfoList;
        showOperateView(operateInfoList);

    }


    public void setCenterOperateInfo(OrderOperateInfo operateInfo) {
        this.centerOperateInfo = operateInfo;
        if (operateInfo != null) {
            setCenterBg(operateInfo.operateImageId);
            setCenterText(operateInfo.operate);
            setShowCenterOperate(true);
        } else {
            setShowCenterOperate(false);
        }
    }

    private void showOperateView(List<OrderOperateInfo> operateInfoList) {
        if (operateInfoList == null) return;
        int operateListSize = operateInfoList.size();
        int operateTextViewListSize = operateTextViewList.size();
        if (operateListSize == operateTextViewListSize) {
            OrderOperateInfo operateInfo;
            TextView operateTextView;
            for (int i = 0; i < operateListSize; i++) {
                operateInfo = operateInfoList.get(i);
                operateTextView = operateTextViewList.get(i);
                operateTextView.setText(operateInfo.operate);
                operateTextView.setEnabled(operateInfo.isEnable);
            }
        } else {
            LogUtil.print("operateListSize not equals operateTextViewListSize");
        }


        int operateImageViewListSize = operateImageList.size();
        if (operateListSize == operateImageViewListSize) {
            OrderOperateInfo operateInfo;
            ImageView operateImageView;
            for (int i = 0; i < operateListSize; i++) {
                operateInfo = operateInfoList.get(i);
                operateImageView = operateImageList.get(i);
                if (operateImageView != null && operateInfo.operateImageId > 0) {
                    operateImageView.setBackgroundResource(operateInfo.operateImageId);
                }
            }
        } else {
            LogUtil.print("operateListSize not equals operateImageViewListSize");
        }


    }

    public interface OnOrderOperateClickListener {
        void onOperateClick(Context context, TextView textView, int position, OrderOperateInfo operateInfo);
    }

    public void setOnOrderOperateClickListener(OnOrderOperateClickListener onOrderOperateClickListener) {
        this.onOrderOperateClickListener = onOrderOperateClickListener;
    }
}
